package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.TransportOrderInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;


/**
 * The type Transportation fragment.
 *
 * @author sHadowLess
 */
public class TransportationFragment extends BaseFragment {

    /**
     * The Transport order no.
     */
    @BindView(R.id.transport_orderNo)
    TextView transportOrderNo;
    /**
     * The Transport make time.
     */
    @BindView(R.id.transport_makeTime)
    TextView transportMakeTime;
    /**
     * The Transport maker.
     */
    @BindView(R.id.transport_maker)
    TextView transportMaker;
    /**
     * The Transport position.
     */
    @BindView(R.id.transport_position)
    TextView transportPosition;
    /**
     * The Transport applier.
     */
    @BindView(R.id.transport_applier)
    TextView transportApplier;
    /**
     * The Transport depart name.
     */
    @BindView(R.id.transport_departName)
    TextView transportDepartName;
    /**
     * The Transport carriers.
     */
    @BindView(R.id.transport_carriers)
    TextView transportCarriers;
    /**
     * The Transport years.
     */
    @BindView(R.id.transport_years)
    TextView transportYears;
    /**
     * The Transport budget item.
     */
    @BindView(R.id.transport_budgetItem)
    TextView transportBudgetItem;
    /**
     * The Transport remark.
     */
    @BindView(R.id.transport_remark)
    TextView transportRemark;
    /**
     * The Transport schedule bill.
     */
    @BindView(R.id.transport_scheduleBill)
    LinearLayout transportScheduleBill;
    /**
     * The Transport budget info.
     */
    @BindView(R.id.transport_budgetInfo)
    TextView transportBudgetInfo;
    /**
     * The Transport check process.
     */
    @BindView(R.id.transport_check_process)
    LinearLayout transportCheckProcess;
    /**
     * The Transport all amount.
     */
    @BindView(R.id.transport_allAmount)
    TextView transportAllAmount;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_transportation_layout;
    }

    private final String url = "api/Pay/LoadPayHistoryList?%s=";
    private final String forMat = "%s\n预算总金额：%s\n已报销：%s\n报销中：%s\n剩余可报销：%s\n已申请未报销：%s\n在申请未报销：%s\n剩余可申请：%s";

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initTransportOrderInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "payId") + orderId, mActivity, transportCheckProcess);
        }
    }

    private void initTransportOrderInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getTransportOrderInfo(orderId, userName, new RequestCallBack<TransportOrderInfo>() {
                    @Override
                    public void success(RequestResult<TransportOrderInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                TransportOrderInfo.DataBean dataBean = result.getResult().getData();
                                TransportOrderInfo.DataBean.BudgetItemObjectBean budgetItemObjectBean = dataBean.getBudgetItemObject();
                                List<TransportOrderInfo.DataBean.DetailListBean> detailListBeans = dataBean.getDetailList();

                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String applicantName = dataBean.getApplicantName();
                                String departName = dataBean.getDepartmentName();
                                transportOrderNo.setText(id);
                                transportMakeTime.setText(createTimeStr);
                                transportMaker.setText(applicantName);
                                transportPosition.setText(departName);


                                for (TransportOrderInfo.DataBean.DetailListBean detailListBean : detailListBeans) {
                                    String carrierName = detailListBean.getCarrierName();
                                    transportCarriers.setText(carrierName);
                                }
                                String applyYear = dataBean.getApplyYear() + "";
                                String budgetItemName = dataBean.getBudgetItemName();
                                String remark = dataBean.getRemark();
                                transportApplier.setText(applicantName);
                                transportDepartName.setText(departName);
                                transportYears.setText(applyYear);
                                transportBudgetItem.setText(budgetItemName);
                                transportRemark.setText(remark);

                                for (TransportOrderInfo.DataBean.DetailListBean detailListBean : detailListBeans) {
                                    View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_transportation_tab_item, null, false);
                                    LinearLayout tab = view.findViewById(R.id.transport_tab_Item);
                                    TextView transportTabTransportOrder = view.findViewById(R.id.transport_tab_transportOrder);
                                    TextView transportTabSendCarDate = view.findViewById(R.id.transport_tab_SendCarDate);
                                    TextView transportTabCarNo = view.findViewById(R.id.transport_tab_CarNo);
                                    TextView transportTabGoLocal = view.findViewById(R.id.transport_tab_GoLocal);
                                    TextView transportTabArriveLocal = view.findViewById(R.id.transport_tab_ArriveLocal);
                                    TextView transportTabFreight = view.findViewById(R.id.transport_tab_Freight);
                                    TextView transportTabRiddingCost = view.findViewById(R.id.transport_tab_RiddingCost);
                                    TextView transportTabActingMatCost = view.findViewById(R.id.transport_tab_ActingMatCost);
                                    TextView transportTabOtherCost = view.findViewById(R.id.transport_tab_OtherCost);
                                    TextView transportTabIsTax = view.findViewById(R.id.transport_tab_IsTax);
                                    TextView transportTabDemandCar = view.findViewById(R.id.transport_tab_DemandCar);
                                    TextView transportTabRealCar = view.findViewById(R.id.transport_tab_RealCar);
                                    TextView transportTabDriver = view.findViewById(R.id.transport_tab_Driver);
                                    TextView transportTabCustomer = view.findViewById(R.id.transport_tab_Customer);
                                    TextView transportTabGoodsType = view.findViewById(R.id.transport_tab_GoodsType);
                                    TextView transportTabRemark = view.findViewById(R.id.transport_tab_remark);
                                    TextView transportTabAll = view.findViewById(R.id.transport_tab_All);
                                    String transportId = detailListBean.getId();
                                    String departureTimeStr = detailListBean.getDepartureTimeStr();
                                    String truckNo = detailListBean.getTruckNo();
                                    String startPlaceName = detailListBean.getStartPlaceName();
                                    String endPlaceName = detailListBean.getEndPlaceName();
                                    String transportMoney = getDecimal(detailListBean.getTransportMoney().doubleValue());
                                    String escortMoney = getDecimal(detailListBean.getEscortMoney().doubleValue());
                                    String prepaidMoney = getDecimal(detailListBean.getPrepaidMoney().doubleValue());
                                    String otherMoney = getDecimal(detailListBean.getOtherMoney().doubleValue());
                                    String isDutyStr = detailListBean.getIsDutyStr();
                                    String needTruckTypeName = detailListBean.getNeedTruckTypeName();
                                    String realTruckTypeName = detailListBean.getRealTruckTypeName();
                                    String truckDriver = detailListBean.getTruckDriver();
                                    String customerName = detailListBean.getCustomerName();
                                    String productInfo = detailListBean.getProductInfo();
                                    String transportRemark = detailListBean.getRemark();
                                    String allMoney = getDecimal(detailListBean.getAllMoney().doubleValue());
                                    transportTabTransportOrder.setText(transportId);
                                    transportTabSendCarDate.setText(departureTimeStr);
                                    transportTabCarNo.setText(truckNo);
                                    transportTabGoLocal.setText(startPlaceName);
                                    transportTabArriveLocal.setText(endPlaceName);
                                    transportTabFreight.setText(transportMoney);
                                    transportTabRiddingCost.setText(escortMoney);
                                    transportTabActingMatCost.setText(prepaidMoney);
                                    transportTabOtherCost.setText(otherMoney);
                                    transportTabIsTax.setText(isDutyStr);
                                    transportTabDemandCar.setText(needTruckTypeName);
                                    transportTabRealCar.setText(realTruckTypeName);
                                    transportTabDriver.setText(truckDriver);
                                    transportTabCustomer.setText(customerName);
                                    transportTabGoodsType.setText(productInfo);
                                    transportTabRemark.setText(transportRemark);
                                    transportTabAll.setText(allMoney);
                                    transportScheduleBill.addView(tab);
                                }

                                String totalCostStr = dataBean.getTotalCostStr();
                                transportAllAmount.setText(totalCostStr);

                                String budgetInfo = budgetItemObjectBean.getBudgetInfo();
                                String allMoney = getDecimal(budgetItemObjectBean.getAllMoney().doubleValue());
                                String reimbursedMoney = getDecimal(budgetItemObjectBean.getReimbursedMoney().doubleValue());
                                String reimbursingMoney = getDecimal(budgetItemObjectBean.getReimbursingMoney().doubleValue());
                                String remainReimburseMoney = getDecimal(budgetItemObjectBean.getRemainReimburseMoney().doubleValue());
                                String applyedMoney = getDecimal(budgetItemObjectBean.getApplyedMoney().doubleValue());
                                String applyingMoney = getDecimal(budgetItemObjectBean.getApplyingMoney().doubleValue());
                                String remainApplyMoney = getDecimal(budgetItemObjectBean.getRemainApplyMoney().doubleValue());
                                String comInfo = String.format(
                                        forMat,
                                        budgetInfo,
                                        allMoney,
                                        reimbursedMoney,
                                        reimbursingMoney,
                                        remainReimburseMoney,
                                        applyedMoney,
                                        applyingMoney,
                                        remainApplyMoney
                                );
                                transportBudgetInfo.setText(comInfo);
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }

    @Override
    protected void initView() {

    }
}
